package com.mzplayer.utils;


import java.io.IOException;
import java.io.InputStream;

import tv.danmaku.ijk.media.player.misc.IMediaDataSource;

public class MediaStream implements IMediaDataSource {
    private InputStream is;
    private long position = 0;

    public MediaStream(InputStream is) throws IOException {
        if(is==null) {
            throw new IOException("InputStream is null");
        }
        this.is = is;
    }

    @Override
    public int readAt(long position, byte[] buffer, int offset, int size) throws IOException {
        if (size <= 0) {
            return size;
        }
        if (this.position != position) {
            is.reset();
            this.position = is.skip(position);
        }
        int length = is.read(buffer, offset, size);
        this.position += length;
        return length;
    }

    @Override
    public long getSize() throws IOException {
        return is.available();
    }

    @Override
    public void close() throws IOException {
        is.close();
        is = null;
    }

}